package com.haochang.chunkvod.app.utils;

import com.haochang.base.EventProxy;
import com.haochang.base.IEventConstant;
import com.haochang.base.VodApplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

/**
 * 
 * @author lxl
 *		2014-12-23-下午12:03:18
 *
 *		网络信息配置:
 *									网络环境检测(wifi,2G/3G/4G,网络状态检查)
 *	
 * @update xjunjie@gmail.com  2015-1-19 下午3:04:38
 * 				取消对公共变量的直接读写.添加 get 和 set 入口.以便支持异步锁 synchronized
 */
public class NetworkUtils
{
	/*
	 * 取消该变量的 public 访问修饰.目的是该变量多线程环境读写时,需要加锁控制
	 */
	private static NetWorkEnum networkType = NetWorkEnum.NETWORK_UNAVAILABLE;

	public NetworkUtils()
	{
	}

	public NetWorkEnum getNetworkType()
	{
		NetWorkEnum returnValue = NetWorkEnum.NETWORK_UNAVAILABLE;
		if (VodApplication.appContext != null)
		{
			ConnectivityManager connectivityManager = (ConnectivityManager) VodApplication.appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
			returnValue = this.getNetworkType(connectivityManager.getActiveNetworkInfo());
		}
		return returnValue;
	}

	public NetWorkEnum getNetworkType(NetworkInfo info)
	{
		NetWorkEnum returnValue = NetWorkEnum.NETWORK_UNAVAILABLE;
		//无网络连接
		if (info != null && info.isAvailable())
		{
			int type = info.getType();
			//判断网络类型
			if (type == ConnectivityManager.TYPE_WIFI)
			{
				returnValue = NetWorkEnum.NetWork_WIFI;
			} else
			{
				String netType = info.getExtraInfo();
				if (!TextUtils.isEmpty(netType))
				{
					//区分 国内 三大主流运营商
					if (netType.equals("cmwap") || netType.equals("cmnet"))
						returnValue = NetWorkEnum.NetWork_CHINA_MOBILE;
					else if (netType.equals("3gwap") || netType.equals("3gnet"))
						returnValue = NetWorkEnum.NetWork_CHN_CUGSM;
					else if (netType.equals("ctwap") || netType.equals("ctwap"))
						returnValue = NetWorkEnum.NetWork_CHINA_TELECOM;
					else
						returnValue = NetWorkEnum.NETWORK_OTHER;
				}
			}
		}
		return returnValue;
	}

	public enum NetWorkEnum
	{
		/**
		 * 移动网络-中国移动
		 */
		NetWork_CHINA_MOBILE,

		/**
		 * 移动网络-中国联通
		 */
		NetWork_CHN_CUGSM,

		/**
		 * 移动网络-中国电信
		 */
		NetWork_CHINA_TELECOM,

		/**
		 *  移动网络-其他
		 */
		NETWORK_OTHER,

		/**
		 * Wifi 无线网络
		 */
		NetWork_WIFI,

		/**
		 * 网络不可用
		 */
		NETWORK_UNAVAILABLE;
	}

	/**
	 * 设置网络状态
	 * @param netWorkEnum
	 */
	public static synchronized void setNetWorkState(NetWorkEnum netWorkEnum)
	{
		networkType = netWorkEnum;
	}

	/**
	 * 获取网络状态.公共查询网络状态
	 * @return 网络状态枚举
	 */
	public static synchronized NetWorkEnum getNetWorkState()
	{
		return networkType;
	}

	private static NetworkConnectBroadcastReceiver mNetworkConnectBroadcastReceiver = null;

	/**
	 * 实时监控.系统网络状态变化
	 * 如果.应用运行过程中,需要实时监听网络状态变化.则在应用启动过程中.调用一次.
	 */
	public static void initRealTime()
	{
		if (VodApplication.appContext != null)
		{
			if (mNetworkConnectBroadcastReceiver == null)
			{
				mNetworkConnectBroadcastReceiver = new NetworkConnectBroadcastReceiver();
				IntentFilter mFilter = new IntentFilter();
				mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
				VodApplication.appContext.registerReceiver(mNetworkConnectBroadcastReceiver, mFilter);
			}
		} else
		{
			throw new RuntimeException();
		}
	}

	private static class NetworkConnectBroadcastReceiver extends BroadcastReceiver
	{
		@Override
		public void onReceive(Context context, Intent intent)
		{
			if (context != null && intent != null)
			{
				String action = intent.getAction();
				if (action != null && action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
				{
					ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
					NetWorkEnum mNetWorkEnum = new NetworkUtils().getNetworkType(connectivityManager.getActiveNetworkInfo());
					
					NetworkUtils.setNetWorkState(mNetWorkEnum);
					connectivityManager = null;
					EventProxy.notifyEvent(IEventConstant.EVENT_NET_STATE_CHANGE, mNetWorkEnum);
				}
				action = null;
			}
		}
	}
}
